﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;

namespace ConcordWinManager
{
    public static class Settings
    {
        public static string rememberLogin;

        public static Consultant consultant = new Consultant();

        public static void Save(string exeConfigName)
        {
            // Takes the executable file name without the
            // .config extension.
            try
            {
                // Open the configuration file and retrieve 
                // the connectionStrings section.
                Configuration config = ConfigurationManager.OpenExeConfiguration(exeConfigName);

                ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;

                //section.SectionInformation.UnprotectSection();

                //section.SectionInformation.UnprotectSection();

                if (!section.SectionInformation.IsProtected)
                {
                    // Encrypt the section.
                    //section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                }

                
                
                // Save the current configuration.
                config.Save();

                Properties.Settings.Default.RememberLogin = rememberLogin;
                Properties.Settings.Default.Save();

                //Console.WriteLine("Protected={0}", section.SectionInformation.IsProtected);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public static void Load()
        {
            DB.sc_str = new SqlConnectionStringBuilder();

            DB.sc_str.DataSource = Properties.Settings.Default.ServerName;
            DB.sc_str.InitialCatalog = Properties.Settings.Default.DatabaseName;
            DB.sc_str.ConnectTimeout = int.Parse(Properties.Settings.Default.ConnectionTimeout);            

            if (Properties.Settings.Default.IntegratedSecurity)
            {
                DB.sc_str.IntegratedSecurity = Properties.Settings.Default.IntegratedSecurity;
            }
            else
            {
                DB.sc_str.UserID = Properties.Settings.Default.Username;
                DB.sc_str.Password = Properties.Settings.Default.Password;
            }

            rememberLogin = Properties.Settings.Default.RememberLogin;
        }


        public static void ToggleConfigEncryption(string exeConfigName)
        {
            // Takes the executable file name without the
            // .config extension.
            try
            {
                // Open the configuration file and retrieve 
                // the connectionStrings section.
                Configuration config = ConfigurationManager.
                    OpenExeConfiguration(exeConfigName);

                ConnectionStringsSection section =
                    config.GetSection("connectionStrings")
                    as ConnectionStringsSection;

                if (section.SectionInformation.IsProtected)
                {
                    // Remove encryption.
      //              section.SectionInformation.UnprotectSection();
                }
                else
                {
                    // Encrypt the section.
                    section.SectionInformation.ProtectSection(
                        "DataProtectionConfigurationProvider");
                }
                // Save the current configuration.
                config.Save();

                Console.WriteLine("Protected={0}",
                    section.SectionInformation.IsProtected);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
